//////////////////////////////////
// Gregory R McNeil
//
// Car Demo
//
//////////////////////////////////

//my files
#include "Camera.h"
#include "Timer.h"
//library files
#include <iostream>
#include <SDL_OpenGL.h>
#include <GL/glu.h>

Camera::Camera()
{
}

void Camera::Prepare()
{
	SetPos(Vec3f(20,4,20));
	SetLookAt(Vec3f(0,0,0));
	gluLookAt(GetPos().GetX(), GetPos().GetY(), GetPos().GetZ(),
		GetLookAt().GetX(), GetLookAt().GetY(), GetLookAt().GetZ(),
		0.0, 1.0, 0);
}

void Camera::Update()
{
	float dt = TheTimer::Instance()->GetDt();
	Vec3f oldVel = m_vel;
	m_pos += (m_vel + oldVel) * 0.5 * dt;

	gluLookAt(TheCamera::Instance()->GetPos().GetX(),
		TheCamera::Instance()->GetPos().GetY(),
		TheCamera::Instance()->GetPos().GetZ(),
		TheCar::Instance()->GetPos().GetX(),
		TheCar::Instance()->GetPos().GetY(),
		TheCar::Instance()->GetPos().GetZ(),
		0.0, 1.0, 0.0);
}
const Vec3f& Camera::GetPos()
{
	return m_pos;
}
const Vec3f& Camera::GetVel()
{
	return m_vel;
}
void Camera::SetPos(Vec3f position)
{
	m_pos = position;
}
const Vec3f& Camera::GetLookAt()
{
	return m_lookAt;
}
void Camera::SetLookAt(Vec3f lookAt)
{
	m_lookAt = lookAt;
}
void Camera::SetCamVel(const Vec3f& camVel)
{
	m_vel = camVel;
}
void Camera::OnKeyboardEvent(const SDL_KeyboardEvent& event)
{
	if(event.state == SDL_PRESSED)
	{
		switch (event.keysym.sym)
		{
		case SDLK_w :
			{
				//move camera forward
				SetPos(Vec3f(GetPos().GetX() + 1, GetPos().GetY(), GetPos().GetZ()));
				break;
			}
		case SDLK_a :
			{
				//move camera left
				SetPos(Vec3f(GetPos().GetX(), GetPos().GetY(), GetPos().GetZ() + 1));
				break;
			}		
		case SDLK_s :
			{
				//move camera back
				SetPos(Vec3f(GetPos().GetX() - 1, GetPos().GetY(), GetPos().GetZ()));
				break;
			}		
		case SDLK_d :
			{
				//move camera right
				SetPos(Vec3f(GetPos().GetX(), GetPos().GetY(), GetPos().GetZ() - 1));
				break;
			}
		case SDLK_e :
			{
				//move camera up
				SetPos(Vec3f(GetPos().GetX(), GetPos().GetY() + 1, GetPos().GetZ()));
				break;
			}		
		case SDLK_c :
			{
				//move camera down
				SetPos(Vec3f(GetPos().GetX(), GetPos().GetY()-1, GetPos().GetZ()));
				break;
			}		}
	}
}

